﻿/*
 * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
 *
 * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
 *
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
 */

#include "WorkThreadPool.h"

namespace toolkit {

// 静态成员变量，用于存储线程池大小和CPU亲和性设置
static size_t s_pool_size = 0;  // 线程池大小，0表示使用默认值
static bool s_enable_cpu_affinity = true;  // 是否启用CPU亲和性

// 实现单例模式
INSTANCE_IMP(WorkThreadPool)

/**
 * 获取第一个EventPoller实例
 * @return 第一个EventPoller的智能指针
 */
EventPoller::Ptr WorkThreadPool::getFirstPoller() {
    return std::static_pointer_cast<EventPoller>(_threads.front());
}

/**
 * 根据负载情况获取EventPoller实例
 * @return 负载较轻的EventPoller的智能指针
 */
EventPoller::Ptr WorkThreadPool::getPoller() {
    return std::static_pointer_cast<EventPoller>(getExecutor());
}

/**
 * 构造函数
 * 初始化工作线程池，创建指定数量的EventPoller线程
 */
WorkThreadPool::WorkThreadPool() {
    // 创建最低优先级的EventPoller线程
    addPoller("work poller", s_pool_size, ThreadPool::PRIORITY_LOWEST, false, s_enable_cpu_affinity);
}

/**
 * 设置线程池大小
 * @param size 线程池大小，0表示使用默认值
 */
void WorkThreadPool::setPoolSize(size_t size) {
    s_pool_size = size;
}

/**
 * 设置是否启用CPU亲和性
 * @param enable 是否启用CPU亲和性
 */
void WorkThreadPool::enableCpuAffinity(bool enable) {
    s_enable_cpu_affinity = enable;
}

} /* namespace toolkit */

